<?php
namespace app_admin\controllers;

use yii\web\Controller;
use yii;

class BaseController extends Controller
{
    public function init()
    {
        //$error=Yii::$app->errorHandler->errorAction;
        //echo json_encode($error);exit;
        $this->checkLogin();
    }

    private function checkLogin()
    {
        $user = isset(Yii::$app->session['user']) ? Yii::$app->session['user'] : '';
        if (empty($user)) {
            header("Content-type: text/html; charset=utf-8");
            header("Cache-Control: no-cache");
            header("Pragma: no-cache");
            header("Location:login");
            exit;
        }
    }

    public function view($view, $_data = array())
    {
        yii::$app->debug->sTime("controller");

        $route = Yii::$app->controller->route;
        list($class, $action) = explode('/', $route);

        $container = new yii\di\Container;
        $container->set('model', "app_admin\\models\\{$class}Model");
        $model = $container->get('model');
        $data = call_user_func_array(array($model, "action{$action}"), func_get_args());
        $data = $data ? $data : array();
        array_push($data, $_data);
//echo json_encode($data);exit;
        yii::$app->debug->eTime("controller");
        return $this->render($view, $data);
    }
    public function viewPopup($view, $_data = array())
    {
        $this->layout = "popup";
        return $this->view($view, $_data);
    }
}
